<template>
  <div class="list-table">
    <div class="area-header">
      <span class="area-header-title">{{ $t('风险管理') }}</span>
    </div>
    <el-form ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item :label="$t('风险名称')" prop="riskName">
        <el-input
          v-model="queryParams.riskName"
          :placeholder="$t('风险名称')"
          clearable
          size="small"
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item :label="$t('风险等级')" prop="riskLevel">
        <el-select v-model="queryParams.riskLevel" :placeholder="$t('风险等级')" clearable size="small">
          <el-option
            v-for="dict in levelOptions"
            :key="dict.dictValue"
            :label="$t(dict.dictLabel)"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('风险类型')" prop="riskType">
        <el-select v-model="queryParams.riskType" :placeholder="$t('风险类型')" clearable size="small">
          <el-option
            v-for="dict in riskTypeOptions"
            :key="dict.dictValue"
            :label="$t(dict.dictLabel)"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('发生概率')" prop="probability">
        <el-select v-model="queryParams.probability" :placeholder="$t('发生概率')" clearable size="small">
          <el-option
            v-for="dict in levelOptions"
            :key="dict.dictValue"
            :label="$t(dict.dictLabel)"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('损失程度')" prop="loss">
        <el-select v-model="queryParams.loss" :placeholder="$t('损失程度')" clearable size="small">
          <el-option
            v-for="dict in levelOptions"
            :key="dict.dictValue"
            :label="$t(dict.dictLabel)"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('风险状态')" prop="riskState">
        <el-select v-model="queryParams.riskState" :placeholder="$t('风险状态')" clearable size="small">
          <el-option
            v-for="dict in riskStateOptions"
            :key="dict.dictValue"
            :label="$t(dict.dictLabel)"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t("搜索") }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t("重置") }}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['deal:risk:edit']"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="addHandle"
        >{{ $t("新增") }}</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['deal:risk:export']"
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="exportHandle"
        >{{ $t("导出") }}</el-button>
      </el-col>
    </el-row>
    <el-table v-loading="loading" tooltip-effect="darkTable" :data="dealRiskList" border>
      <el-table-column :label="$t('序号')" type="index" align="center" show-overflow-tooltip width="50">
        <template slot-scope="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('风险名称')" align="center" prop="riskName" show-overflow-tooltip>
        <template slot-scope="scope">
          <span class="link-type" @click="updateHandle(scope.row)">{{ scope.row.riskName }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('风险等级')" align="center" prop="riskLevel" :formatter="levelFormat" show-overflow-tooltip width="110" />
      <el-table-column :label="$t('风险类型')" align="center" prop="riskType" :formatter="riskTypeFormat" show-overflow-tooltip width="110" />
      <el-table-column :label="$t('发生概率')" align="center" prop="probability" :formatter="probabilityFormat" show-overflow-tooltip width="110" />
      <el-table-column :label="$t('损失程度')" align="center" prop="loss" :formatter="lossFormat" show-overflow-tooltip width="110" />
      <el-table-column :label="$t('风险状态')" align="center" prop="riskState" :formatter="riskStateFormat" show-overflow-tooltip width="110" />
      <el-table-column :label="$t('发生时间')" align="center" prop="riskTime" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.riskTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('风险事项描述')" align="center" prop="remark" show-overflow-tooltip />
      <!-- <el-table-column :label="$t('关闭原因')" align="center" prop="closeCause" show-overflow-tooltip /> -->
      <el-table-column v-if="isShow" :label="$t('操作')" align="center" class-name="small-padding fixed-width" width="150">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['deal:risk:edit']"
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="updateHandle(scope.row)"
          >{{ $t('修改') }}</el-button>
          <el-button
            v-hasPermi="['deal:risk:remove']"
            size="small"
            type="text"
            icon="el-icon-delete"
            @click="deleteHandle(scope.row)"
          >{{ $t('删除') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改风险管理对话框 -->
    <el-dialog :title="$t(title)" :visible.sync="formOpen" :close-on-click-modal="false" :lock-scroll="false" custom-class="dialogForm" append-to-body>
      <dealRiskForm ref="dealRiskForm" :deal-id="dealId" />
    </el-dialog>
  </div>
</template>

<script>
import { listDealRisk, delDealRisk } from '@/api/deal/risk/dealRisk'
import dealRiskForm from './dealRiskForm.vue'
export default {
    components: {
        dealRiskForm
    },
  props: {
      dealId: {
        type: String,
        default: undefined
      }
  },
  data() {
      return {
          // 遮罩层
          loading: true,
          // 总条数
          total: 0,
          isShow: true,
          // 风险管理表格数据
          dealRiskList: [],
          // 弹出层标题
          title: '风险管理',
          // 是否显示弹出层
          formOpen: false,
        // 风险等级、发生概率、损失程度 字典level字典
        levelOptions: [],
        // 风险类型 字典risk_type字典
        riskTypeOptions: [],
        // 风险状态 字典risk_status字典
        riskStateOptions: [],
          // 查询参数
          queryParams: {
              dealId: undefined,
              riskName: undefined,
              riskLevel: undefined,
              riskType: undefined,
              probability: undefined,
              loss: undefined,
              riskState: undefined,
              riskTime: undefined,
              pageNum: 1,
              pageSize: 10
          },
          // 表单参数
          form: {},
          // 表单校验
          rules: {}
      }
  },
  created() {
      this.$nextTick(() => {
        this.getList()
      })
      this.getDicts('level').then(response => {
          this.levelOptions = response.data
      })
      this.getDicts('risk_type').then(response => {
          this.riskTypeOptions = response.data
      })
      this.getDicts('risk_status').then(response => {
          this.riskStateOptions = response.data
      })
  },
  methods: {
    /** 查询风险管理列表 */
    getList() {
        this.loading = true
        this.$nextTick(() => {
          this.queryParams.dealId = this.$route.query.id || this.dealId
        })
        listDealRisk(this.queryParams).then(response => {
            this.dealRiskList = response.rows
            this.total = response.total
            this.loading = false
        })
    },
    // 风险等级 字典level字典翻译
    levelFormat(row) {
      return this.selectDictLabel(this.levelOptions, row.riskLevel)
    },
    probabilityFormat(row) {
      return this.selectDictLabel(this.levelOptions, row.probability)
    },
    lossFormat(row) {
      return this.selectDictLabel(this.levelOptions, row.loss)
    },
    // 风险状态 字典risk_status字典翻译
    riskStateFormat(row) {
      return this.selectDictLabel(this.riskStateOptions, row.riskState)
    },
    // 风险类型
    riskTypeFormat(row) {
      return this.selectDictLabel(this.riskTypeOptions, row.riskType)
    },
    // 表单重置
    reset() {
        this.form = {
            id: undefined,
            riskName: undefined,
            riskLevel: undefined,
            riskType: undefined,
            probability: undefined,
            loss: undefined,
            riskState: undefined,
            riskTime: undefined,
            remark: undefined,
            closeCause: undefined,
            createBy: undefined,
            createTime: undefined,
            updateBy: undefined,
            updateTime: undefined,
            delFlag: undefined
        }
        this.resetForm('form')
    },
    /** 搜索按钮操作 */
    queryHandle() {
        this.queryParams.pageNum = 1
        this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
        this.resetForm('queryForm')
        this.queryHandle()
    },
    /** 新增按钮操作 */
    addHandle() {
        this.formOpen = true
        this.$nextTick(() => {
            this.$refs.dealRiskForm.init(null)
        })
    },
    /** 修改按钮操作 */
    updateHandle(row) {
        this.formOpen = true
        this.$nextTick(() => {
            // 调用form页面的 init 方法初始化数据
            this.$refs.dealRiskForm.init(row)
        })
    },
    /** 删除按钮操作 */
    deleteHandle(row) {
        this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
            confirmButtonText: this.$t('确定'),
            cancelButtonText: this.$t('取消'),
            type: 'warning'
        }).then(function() {
            return delDealRisk(row.id)
        }).then(() => {
            this.getList()
            this.msgSuccess(this.$t('删除成功'))
        }).catch(function() {
        })
    },
    /** 导出按钮操作 */
    exportHandle() {
        this.downLoadExcel('/risk/dealRisk/export', this.queryParams)
    }
  }
}
</script>
